#pragma once

#define _CRT_SECURE_NO_WARNINGS
#pragma once
#include<iostream>
#include<assert.h>
using namespace std;

namespace fast
{
	class string
	{
	public:

		string(const char* str = "")
		{
			size_t len = strlen(str);
			_str = new char[len + 1];
			_size = _capacity = len;
			strcpy(_str, str);
		}
		~string()
		{
			delete[] _str;
			_str = nullptr;
			_size = _capacity = 0;
		}
		string(const string& str)
		{
			delete[] _str;
			_str = new char[str._capacity + 1];
			strcpy(_str, str._str);
			_size = str._size;
			_capacity = str._capacity;
		}
		string& operator=(const string& str)
		{
			if (this != &str)
			{
				delete[] _str;
				_str = new char[str._capacity + 1];
				strcpy(_str, str._str);
				_size = str._size;
				_capacity = str._capacity;
			}
			return *this;
		}
		const char* c_str() const
		{
			return _str;
		}
		size_t size() const
		{
			return _size;
		}
		const char& operator[](int pos) const
		{
			assert(pos <= _size && pos >= 0);
			return _str[pos];
		}
		char& operator[](int pos)
		{
			assert(pos <= _size && pos >= 0);
			return _str[pos];
		}
		void reserve(size_t n)
		{
			if (n > _capacity)
			{
				char* tmp = new char[n + 1];
				strcpy(tmp, _str);
				delete[] _str;
				_str = tmp;
				_capacity = n;
			}
		}
		void push_back(char ch)
		{
			if (_size == _capacity)
			{
				size_t newCapacity = _capacity == 0 ? 4 : _capacity * 2;
				reserve(newCapacity);
			}
			_str[_size] = ch;
			++_size;
			_str[_size] = '\0';
		}
		void push_back(const char*str)
		{
			append(str);
		}
		void append(const char* str)
		{
			size_t len = strlen(str);
			if (len + _size > _capacity)
			{
				reserve(len + _size);
			}
			strcpy(_str + _size, str);
			_size += len;
		}
		string& operator+=(char ch)
		{
			//push_back(ch);
			if (_size == _capacity)
			{
				size_t newCapacity = _capacity == 0 ? 4 : _capacity * 2;
				reserve(newCapacity);
			}
			_str[_size] = ch;
			++_size;
			_str[_size] = '\0';
			return *this;
		}
		string& operator+=(const char* str)
		{
			//append(str);
			size_t len = strlen(str);
			if (len + _size > _capacity)
			{
				reserve(len + _size);
			}
			strcpy(_str + _size, str);
			_size += len;
			return *this;
		}
		void insert(size_t pos, char ch)
		{
			assert(pos <= _size);
			if (_size == _capacity)
			{
				size_t newCapacity = _capacity == 0 ? 4 : _capacity * 2;
				reserve(newCapacity);
			}
			size_t end = _size + 1;
			while (end > pos)
			{
				_str[end] = _str[end - 1];
				--end;
			}
			_str[pos] = ch;
			++_size;
		}
		void insert(size_t pos, const char* str)
		{
			assert(pos <= _size);
			size_t len = strlen(str);
			if (_size + len > _capacity)
			{
				reserve(_size + len);
			}
			size_t end = _size + len;
			while (end > pos)
			{
				_str[end] = _str[end - len];
				--end;
			}
			strncpy(_str + pos, str, len);
			_size += len;
		}
		void erase(size_t pos, size_t len = npos)
		{
			assert(pos < _size);
			if (len == npos || len + pos >= _size)
			{
				_str[pos] = '\0';
				_size = pos;
			}
			else
			{
				strcpy(_str + pos, _str + pos + len);
				_size -= len;
			}
			
		}
		void swap(string& s)
		{
			std::swap(_str, s._str);
			std::swap(_size, s._size);
			std::swap(_capacity, s._capacity);
		}
		size_t find(const char* str, size_t pos = 0)
		{
			assert(pos < _size);
			char* ptr = strstr(_str + pos, str);
			if (ptr == nullptr)
				return npos;
			else
				return ptr - _str;
		}
		string substr(size_t pos, size_t len = npos)
		{
			assert(pos < _size);
			string ret;
			if (len == npos || pos + len >= _size)
			{
				ret += _str + pos;
			}
			else
			{
				size_t i = 0;
				for (i = 0; i < len; i++)
				{
					ret += _str[pos + i];
				}
			}
			return ret;
		}
		typedef char* iterator;
		iterator begin()
		{
			return _str;
		}
		iterator end()
		{
			return _str + _size;
		}
		typedef const char* const_iterator;
		const_iterator const_begin() const
		{
			return _str;
		}
		const_iterator const_end() const
		{
			return _str + _size;
		}
		void print_str();

	private:
		char* _str;
		size_t _size;
		size_t _capacity;

		//const static size_t npos = -1;
		const static size_t npos = -1;
	};

	void test_string()
	{
		string s1("https://legacy.cplusplus.com/reference/string/basic_string/substr/");
		string sub1, sub2, sub3;
		size_t pos1 = s1.find(":");
		sub1 = s1.substr(0, pos1);
		cout << sub1.c_str() << endl;
		size_t pos2 = s1.find("/", pos1 + 3);
		sub2 = s1.substr(pos1 + 3, pos2 - pos1 - 3);
		cout << sub2.c_str() << endl;
		sub3 = s1.substr(pos2);
		cout << sub3.c_str() << endl;
	}
	
}




